package com.sita.tboard.ui.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import com.sita.bike.R;
import com.sita.bike.support.Constants;
import com.sita.bike.utils.AccountUtils;
import com.sita.tboard.ActivityBase;
import com.sita.tboard.roadtrust.discovery.RtResourceListFragment;
import com.sita.tboard.ui.tools.AudioPlayer;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MyRtResourcesActivity extends ActivityBase {

    private AudioPlayer player;
    private ActionBar actionBar;
    @Bind(R.id.toolbar)
    Toolbar mToolbar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_my_rtresource_list);
        ButterKnife.bind(this);
        mToolbar.setTitleTextColor(getResources().getColor(R.color.white));
        setSupportActionBar(mToolbar);
        actionBar = getSupportActionBar();
        actionBar.setTitle(null);
        actionBar.setDisplayHomeAsUpEnabled(true);
        mToolbar.setNavigationIcon(R.mipmap.ic_back);
        player = getPlayer();
        initView();

    }

    private void initView() {
        Fragment fragment = new RtResourceListFragment();
        Bundle bundle = new Bundle();
        bundle.putString(Constants.BUNDLE_ARGUMENT_AUTHOR_ID, AccountUtils.getAccountID());
        fragment.setArguments(bundle);
        // change the fragment
        FragmentTransaction ftx = getSupportFragmentManager().beginTransaction();
        ftx.replace(R.id.frame_container_personal_trends, fragment).commit();

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            case R.id.action_settings:
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (player != null) {
            player.stop();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (player.isPlaying()) {
            player.pause();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    private AudioPlayer getPlayer() {
        if (player != null) {
            if (player.isPlaying())
                player.stop();
        } else {
            player = new AudioPlayer();
        }
        //if everything ok play file
        //in case of any error return from method before (catch)
        return player;
    }

}

